package com.spring.demo8;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

public class StudentJdbcTemplate implements StudentDao {
	private DataSource dataSource;
	private JdbcTemplate jdbcTemplateObject;
	@Override
	public void setDataSource(DataSource ds) {
		this.dataSource = ds;
		this.jdbcTemplateObject = new JdbcTemplate(dataSource);
	}

	@Override
	public void create(String name, Integer age) {
		String sql = "insert into Student(name,age)values(?,?)";
		int flag = jdbcTemplateObject.update(sql, name,age);
		System.out.println(flag+"Created Record Name = " + name + " Age = " + age);
	}

	@Override
	public Student getStudent(Integer id) {
		String SQL = "select * from Student where id = ?";
		Student student = jdbcTemplateObject.queryForObject(SQL,new Object[]{id}, new StudentMapper());
		return student;
	}

	@Override
	public List<Student> listStudents() {
		String SQL = "select * from Student";
		List <Student> students = jdbcTemplateObject.query(SQL,new StudentMapper());
		return students;
	}

	@Override
	public void delete(Integer id) {
		String SQL = "delete from Student where id = ?";
		int flag = jdbcTemplateObject.update(SQL, id);
		System.out.println(flag+"Deleted Record with ID = " + id );	
	}

	@Override
	public void update(Integer id, Integer age) {
		String SQL = "update Student set age = ? where id = ?";
		int flag = jdbcTemplateObject.update(SQL, age, id);
		System.out.println(flag+"Updated Record with ID = " + id );
	}

}
